package java_demos.java_io.create_file;

import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import static java.nio.charset.StandardCharsets.UTF_8;

public class CreateFilePrintWriter {
    public static void main(String[] args) {
        // 创建文件路径
        String filePath = "create_file_printwriter.txt";

        // 创建printwriter对象写入文件
        try (PrintWriter printWriter = new PrintWriter(filePath, String.valueOf(StandardCharsets.UTF_8))) {
           printWriter.println("Hello, PrintWriter!");

           printWriter.println("This is a new line.");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
